import { http } from "@/utils/http";
import { baseUrlApi, addPageConfig } from "./utils";
// import { ref } from "vue";

/** 基础响应类型 */
export type BaseRespon = {
  err: {
    code: number;
    msg: string;
  };
  status: {
    code: number;
    msg: string;
  };
};

export type UserResult = {
  success: boolean;
  data: {
    /** 头像 */
    avatar: string;
    /** 用户名 */
    username: string;
    /** 昵称 */
    nickname: string;
    /** 当前登录用户的角色 */
    roles: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};
export type testUserResult = {
  err: {
    code: number;
    msg: string;
  };
  status: {
    code: number;
    msg: string;
  };
  tokens: {
    refreshtoken: string;
    accesstoken: string;
  };
};

export type RefreshTokenResult = {
  success: boolean;
  tokens: {
    accesstoken: string;
    refreshtoken: string;
    expires: Date;
  };
};

/** 搜索用户 */
export type SearchUserResult = BaseRespon & {
  result: {
    total: number;
    /** 用户列表 */
    data: Array<{
      /** 用户id */
      userid: number;
      /** 邮箱 */
      useremail: string;
      /** 机构名称 */
      unitname: string;
      /** 课题组名称 */
      rpgname: string;
      /** 用户名 */
      username: string;
      /** qq */
      qq: string;
      /** 手机号码 */
      phone: string;
      /** 积分 */
      point: number;
      /** 最后登录时间 */
      lastlogintime: string;
      /** 账号状态 */
      enable: boolean;
    }>;
  };
};

/** 登录 */
export type LoginResult = {
  success: boolean;
  data: {
    /** 头像 */
    avatar: string;
    /** 用户名 */
    username: string;
    /** 昵称 */
    nickname: string;
    /** 当前登录用户的角色 */
    roles: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
  };
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", "/login", { data });
};

/** 刷新`token` */
export const refreshTokenApi = (refreshtoken?: string) => {
  return http.request<RefreshTokenResult>("get", baseUrlApi(`refreshlogin`), {
    params: {
      refreshtoken: refreshtoken
    }
  });
};

/** 获取用户信息 */
export const getUser = (data?: object) => {
  return http.request<SearchUserResult>(
    "post",
    baseUrlApi("user/search"),
    addPageConfig({
      data
    })
  );
};

export const testLogin = (data?: object) => {
  return http.request<any>("post", baseUrlApi("login"), { data });
};

/** 登录测试接口 */
export type UserResultText = {
  err: {
    code: number;
    msg: string;
  };
  status: {
    code: number;
    msg: string;
  };
  tokens: {
    accesstoken: string;
    refreshtoken: string;
    expires: Date;
  };
};

export const getLoginText = (data?: object) => {
  return http.request<UserResultText>("post", baseUrlApi("login"), { data });
};

/** 获取用户信息 */
export const getUserText = (data?: object) => {
  return http.request<any>("post", baseUrlApi("user/search"), { data });
};

/** 查询用户积分 */
export type UserPoint = BaseRespon & {
  data: {
    /** 积分 */
    point: number;
  };
};
export const getUserPoint = (id: string) => {
  return http.request<UserPoint>("get", baseUrlApi("user/point/" + id));
};

/** 添加用户 */
export const addUserAccount = (data?: object) => {
  return http.request<any>("post", baseUrlApi("user"), { data });
};

/** 修改用户 */
export const updateUserAccount = (data?: object) => {
  return http.request<any>("put", baseUrlApi("user"), { data });
};
